package com.drakulo.hi.logic.helpers;

import java.util.List;
import java.util.Map;

import com.dk.hi.core.Team;
import com.drakulo.hi.gfx.Item;
import com.drakulo.hi.gfx.panel.InventoryPanel;
import com.drakulo.hi.logic.actors.Hero;
import com.drakulo.hi.logic.inventory.Inventory;
import com.drakulo.hi.logic.inventory.equipment.Equipment;
import com.drakulo.hi.logic.inventory.equipment.EquipmentType;
import com.drakulo.hi.logic.inventory.equipment.Weapon;

public abstract class InventoryHelper {

	public static int[] getInventoryCoord(Inventory inventory, int originX, int originY, int mouseX, int mouseY) {
		final int oX = originX + (InventoryPanel.SUBPANEL_WIDTH / 2 - (Inventory.COLS * Item.SPRITE_SIZE) / 2);
		int currentX = oX;
		int currentY = originY;

		for (int r = 0; r < Inventory.ROWS; r++) {
			for (int c = 0; c < Inventory.COLS; c++) {
				if (MouseHelper.mouseIsInRect(mouseX, mouseY, currentX, currentY, currentX + Item.SPRITE_SIZE, currentY + Item.SPRITE_SIZE)) {
					return new int[] { c, r };
				}
				currentX += Item.SPRITE_SIZE;
			}
			currentY += Item.SPRITE_SIZE;
			currentX = oX;
		}
		return null;
	}

	public static Equipment tryEquipmentDrag(int mouseX, int mouseY) {
		List<Hero> heroes = Team.getHeroes();
		Equipment e = null;
		for (Hero h : heroes) {
			Map<EquipmentType, Equipment> equipments = h.getEquipments();
			// int deltaX = Team.getHeroDisplayX(h);
			// if(MouseHelper.mouseIsInEquipmentSquare(mouseX, mouseY,
			// EquipmentGfx.HELMET_X,
			// EquipmentGfx.HELMET_Y, deltaX)){
			// // Casque
			// }else if(MouseHelper.mouseIsInEquipmentSquare(mouseX, mouseY,
			// EquipmentGfx.RIGHT_HAND_X, EquipmentGfx.RIGHT_HAND_Y, deltaX)){
			// // Main droite
			// e = equipments.get(EquipmentType.WEAPON);
			// h.wearEquipment(null, EquipmentType.WEAPON);
			// return e;
			// } else if(MouseHelper.mouseIsInEquipmentSquare(mouseX, mouseY,
			// EquipmentGfx.LEFT_HAND_X, EquipmentGfx.LEFT_HAND_Y, deltaX)){
			// // Armure
			// }else if(MouseHelper.mouseIsInEquipmentSquare(mouseX, mouseY,
			// EquipmentGfx.LEFT_HAND_X, EquipmentGfx.LEFT_HAND_Y, deltaX)){
			// // Main gauche
			// } else if(MouseHelper.mouseIsInEquipmentSquare(mouseX, mouseY,
			// EquipmentGfx.BOOTS_X,
			// EquipmentGfx.BOOTS_Y, deltaX)){
			// // Bottes
			// }
		}
		return null;
	}

	public static Item tryItemDrag(int mouseX, int mouseY) {
		Item selectedItem = null;
		int[] coord = null;
		for (Hero h : Team.getHeroes()) {
			Inventory inventory = h.getInventory();
			// coord = InventoryHelper.getInventoryCoord(inventory,
			// Team.getHeroDisplayX(h),
			// InventoryPanelHeroGfx.INVENTORY_Y, mouseX, mouseY);
			// if(coord != null){
			// selectedItem = inventory.pop(coord[0], coord[1]);
			// break;
			// }
		}
		return selectedItem;
	}

	public static Item tryItemDrop(Hero h, Item dropItem, int mouseX, int mouseY) {
		if (h == null) {
			return dropItem;
		}
		Inventory inventory = h.getInventory();
		// int[] coord = InventoryHelper.getInventoryCoord(inventory,
		// Team.getHeroDisplayX(h),
		// InventoryPanelHeroGfx.INVENTORY_Y, mouseX, mouseY);
		// if(coord != null){
		// Item item = inventory.pop(coord[0], coord[1]);
		// inventory.put(coord[0], coord[1], dropItem);
		// return item;
		// }
		return null;
	}

	public static Equipment tryWeaponDrop(Hero h, Weapon dropWeapon, int mouseX, int mouseY) {
		if (h == null) {
			return dropWeapon;
		}
		Equipment lastWeapon = null;

		// int heroX = Team.getHeroDisplayX(h);
		//
		// int topX = heroX + EquipmentGfx.RIGHT_HAND_X;
		// int topY = EquipmentGfx.RIGHT_HAND_Y;
		// int bottomX = topX + Item.SPRITE_SIZE;
		// int bottomY = topY + Item.SPRITE_SIZE;
		// if(MouseHelper.mouseIsInRect(mouseX, mouseY, topX, topY, bottomX,
		// bottomY)){
		// lastWeapon = h.wearEquipment(dropWeapon, EquipmentType.WEAPON);
		// }else{
		// lastWeapon = dropWeapon;
		// }
		return lastWeapon;
	}

	// private static Equipment getEquipmentAt(int mouseX, int MouseY, Hero h){
	// int deltaX = Team.getHeroDisplayX(h);
	// Map<EquipmentType, Equipment> equipments = h.getEquipments();
	//
	// if(mouseIsOnHelmet(mouseX, MouseY, h)){
	// // Casque
	// }
	//
	// return null;
	// }
}
